<template>
	<section class="vbox">
		<section class="scrollable padder">
			<section class="panel panel-default m-t-sm">
				<header class="panel-heading">
					组织机构
					<ul class="nav nav-tabs pull-right no-border">
						<li class="m-r-sm m-t-xs">
							<el-input placeholder="输入组织机构名称" v-model="keywords" clearable>
								<template #append>
									<el-button slot="append" type="success" icon="icon-magnifier icon" @click="query(1)"></el-button>
								</template>
							</el-input>
						</li>
						<li class="m-r-md m-t-xs">
							<button class="btn btn-success" @click="model={}; dialogFormVisible = true"><i class="fa fa-plus-circle"></i> 新增</button>
						</li>
					</ul>
				</header>
				<div class="panel-body">
					<el-table size="medium" :data="orgList" stripe style="width: 100%" border>
						<el-table-column fixed type="index" width="auto" :index="indexMethod">
						</el-table-column>
						<el-table-column prop="orgName" label="组织机构名称" width="200">
						</el-table-column>
						<el-table-column prop="orgCode" label="组织机构编码" width="150">
						</el-table-column>
						<el-table-column prop="leader" label="负责人">
						</el-table-column>
						<el-table-column prop="phone" label="联系电话">
						</el-table-column>
						<el-table-column prop="email" label="邮箱">
						</el-table-column>
						<el-table-column prop="displayNum" label="显示顺序">
						</el-table-column>
						<el-table-column align="center" label="是否可用" width="80">
							<template v-slot="scope">
								<el-switch v-model="scope.row.isEnabled" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
								 inactive-value="0">
								</el-switch>
							</template>
						</el-table-column>
						<el-table-column align="center" label="操作" width="150">
							<template v-slot="scope">
								<el-button type="text" @click="edit(scope.row)">编辑</el-button>
								<el-divider direction="vertical"></el-divider>
								<el-button type="text" @click="deleteOrg(scope.row.id)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>

					<pagination :total="total" :pageNo='pageNo' :pageSize='pageSize' @onPageChange="query"></pagination>
				</div>

				<el-dialog :title="model.id==null ? '新增': '编辑'" v-model="dialogFormVisible" top="30px">
					<form class="form-horizontal" action="#" method="get" onsubmit="return false;">
						<div class="form-group">
							<label class="col-sm-2 control-label">上级组织机构</label>
							<div class="col-sm-10">
								<el-select v-model="model.dataScope" clearable>
									<el-option v-for="item in dataScopes" :key="item.code" :label="item.name" :value="item.code">
									</el-option>
								</el-select>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">组织机构名称</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.orgName" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">组织机构编码</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.orgCode" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">负责人</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.leader" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">联系电话</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.phone" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">邮箱</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.email" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">显示顺序</label>
							<div class="col-sm-10">
								<el-input-number v-model="model.displayNum" :min="1"></el-input-number>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">是否可用</label>
							<div class="col-sm-10">
								<el-switch v-model="model.isEnabled" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
								 inactive-value="0">
								</el-switch>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<div class="col-sm-10 col-sm-offset-2">
								<button type="submit" class="btn btn-success" @click="submit()">提交</button>
							</div>
						</div>
					</form>
				</el-dialog>
			</section>
		</section>
	</section>

</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				dialogFormVisible: false,
				orgList: [],
				total: 0, // 记录总条数
				pageSize: 10, // 每页显示条数
				pageNo: 1, // 当前的页数
				keywords: '',
				model: {},
				dataScopes: [{
					name: '全部数据权限',
					code: '0'
				}, {
					name: '本部门数据权限',
					code: '1'
				}, {
					name: '本部门及以下数据权限',
					code: '2'
				}, {
					name: '自定数据权限',
					code: '3'
				}]
			}
		},
		mounted() {
			this.query(1);
		},
		methods: {
			indexMethod(index) {
				return this.pageSize * (this.pageNo - 1) + index + 1;
			},
			submit() {
				if (this.model.id == null) {
					this.$api.org.add(this.model).then(data => {
						this.model.id = data.id;
						this.query(1);
					});
				} else {
					this.$api.org.update(this.model);
				}
				this.dialogFormVisible = false;
			},
			query(pageNo) {
				this.pageNo = null == pageNo ? 1 : pageNo;
				this.$api.org.query({
					orgName: this.keywords,
					pageSize: this.pageSize,
					pageNo: this.pageNo
				}).then(data => {
					this.orgList = data.rows;
					this.pageNo = data.pageNo;
					if (1 == this.pageNo) {
						this.total = data.total;
					}
				});
			},
			edit(model) {
				this.model = model;
				this.dialogFormVisible = true;
			},
			deleteOrg(id) {
				this.$confirm('您确定要执行删除操作吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$api.org.delete(id).then(data => {
						this.query(this.pageNo);
					});
				});
			}
		}
	}
</script>

<style>
</style>
